<?php include "db.php"; ?>

<?php

class User
{
	private $user_id;
	private $first_name;
	private $last_name;
	private $password;
	private $email;
	private $permissions;
	private $chapter_id;
	
	private $userfound;
	
	public 	function __construct($email,$password) {
		global $mysqli;
		$email = mysqli_real_escape_string($mysqli,$_POST['email']);
		$password = mysqli_real_escape_string($mysqli,$_POST['password']);
		$query = "SELECT * FROM users WHERE Email = '".$email."' AND Password = '".$password."'";
		$data = $mysqli->query($query);   
		if($data->num_rows!=0) {
			$row = $data->fetch_array(MYSQLI_ASSOC);
			echo "ROW = ".$row."<br>";
			$this->first_name=$row["First_Name"];
			$this->last_name=$row["Last_Name"];
			$this->email=$row["Email"];
			$this->permissions=intval($row["PermissionLevel"]);
			$this->user_id=intval($row["User_Id"]);
			$this->chapter_id=intval($row["Chapter_Id"]);
			$this->password=$row["Password"];
			$this->userfound=True;
		}
		else {
			$this->userfound=False;
		}
	}
	
	public function getFirstName() {
		return $this->first_name;
	}
	
	public function getLastName() {
		return $this->last_name;
	}
	
	public function getEmail() {
		return $this->email;
	}
	
	public function getPermissions() {
		return $this->permissions;
	}
	
	public function getUserId() {
		return $this->user_id;
	}
	
	public function getChapterId() {
		return $this->chapter_id;
	}
	
	public function getPassword() {
		return $this->password;
	}
	
	public function userFound() {
		return $this->userfound;
	}
	
	private function updateValue($value,$dbstring){
		global $mysqli;
		$query = "UPDATE users SET ".$dbstring."='".$value."' WHERE User_Id='".$this->user_id."'";
		echo $query;
		echo "<br>";
		$mysqli->query($query);
	}
	
	public function setFirstName($fname) {
		$this->updateValue($fname,"First_Name");
		$this->first_name=$fname;
	}
	
	public function setLastName($lname) {
		$this->updateValue($lname,"Last_Name");
		$this->last_name=$lname;
	}
	
	public function setEmail($email) {
		$this->updateValue($email,"Email");
		$this->email=$email;
	}
	
	public function setPermissions($permissions) {
		$this->updateValue($permissions,"PermissionLevel");
		$this->permissions=$permissions;
	}
	
	public function setPassword($password) {
		$this->updateValue($password,"Password");
		$this->password=$password;
	}
}
?>